//
//  GoodsTableViewHeaderView.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/24.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "GoodsTableViewHeaderView.h"

@interface GoodsTableViewHeaderView ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UIButton * allButton;
@property (nonatomic, strong)UIButton * newsButton;
//@property (nonatomic, strong)UIButton * historyButton;
@property (nonatomic, strong)UIView * allLineView;
@property (nonatomic, strong)UIView * newsLineView;
//@property (nonatomic, strong)UIView * historyLineView;

@end

@implementation GoodsTableViewHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    self.allButton = [self setupButtonWithTitle:@"所有"];
    self.allButton.selected = YES;
    [self addSubview:self.allButton];
    self.newsButton = [self setupButtonWithTitle:@"已发布"];
    [self addSubview:self.newsButton];
//    self.historyButton = [self setupButtonWithTitle:@"草稿"];
//    [self addSubview:self.historyButton];
    
    self.allLineView = [self setupLineView];
    self.allLineView.hidden = NO;
    [self addSubview:self.allLineView];
    self.newsLineView = [self setupLineView];
    [self addSubview:self.newsLineView];
//    self.historyLineView = [self setupLineView];
//    [self addSubview:self.historyLineView];
}

- (void)layoutSubviews{
    
    [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self).offset(30 * SCREENH6);
    }];
    
    [self.allLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.allButton);
        make.height.mas_equalTo(3 * SCREENH6);
        make.top.mas_equalTo(self.allButton.mas_bottom).offset(2 * SCREENH6);
    }];
    
    [self.newsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.allButton);
        make.leading.mas_equalTo(self.allButton.mas_trailing).offset(30 * SCREENH6);
    }];
    
    [self.newsLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.newsButton);
        make.height.centerY.mas_equalTo(self.allLineView);
    }];
    
//    [self.historyButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.newsButton);
//        make.leading.mas_equalTo(self.newsButton.mas_trailing).offset(30 * SCREENH6);
//    }];
//
//    [self.historyLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.mas_equalTo(self.historyButton);
//        make.height.centerY.mas_equalTo(self.newsLineView);
//    }];
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xFFE055);
    lineView.hidden = YES;
    
    return lineView;
}


- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:ZRBlackColor forState:UIControlStateSelected];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    button.selected = NO;
    
    return button;
}


- (void)clickChangeButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.allButton) {
        
        type = @"all";
        
        if (!button.selected) {
            
            self.allButton.selected = YES;
            self.allLineView.hidden = NO;
            self.newsButton.selected = NO;
            self.newsLineView.hidden = YES;
//            self.historyButton.selected = NO;
//            self.historyLineView.hidden = YES;
        }
    }else if (button == self.newsButton){
        
        type = @"news";
        
        if (!button.selected) {
            
            self.allButton.selected = NO;
            self.allLineView.hidden = YES;
            self.newsButton.selected = YES;
            self.newsLineView.hidden = NO;
//            self.historyButton.selected = NO;
//            self.historyLineView.hidden = YES;
        }
    }
//    else if (button == self.historyButton){
//        
//        if (!button.selected) {
//            
//            self.allButton.selected = NO;
//            self.allLineView.hidden = YES;
//            self.newsButton.selected = NO;
//            self.newsLineView.hidden = YES;
//            self.historyButton.selected = YES;
//            self.historyLineView.hidden = NO;
//        }
//    }
    
    if (self.goodsTableViewHeaderViewDelegate && [self.goodsTableViewHeaderViewDelegate respondsToSelector:@selector(clickItemButton:)])
    {
        [self.goodsTableViewHeaderViewDelegate clickItemButton:type];
    }
}

@end
